package com.handpay.zztong.hp.setting.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.log.PlainLogger;
import com.handpay.zztong.hp.log.Storage;
import com.handpay.zztong.hp.log.StorageUtility;
import com.handpay.zztong.hp.log.SystemUtility;
import com.handpay.zztong.hp.other.activity.ShowHtmlActivity;
import com.handpay.zztong.hp.setting.model.accountexit.AccountExitModel;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.io.File;
import java.util.Hashtable;

public class SettingActivity extends ZZTongActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        findViewById(R.id.setting_help).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                Intent intent1 = new Intent(
                                        SettingActivity.this, ShowHtmlActivity.class);
                                intent1.putExtra(ShowHtmlActivity.ShowHtml_Title_Key,
                                        getString(R.string.Help_center));
                                intent1.putExtra(
                                        ShowHtmlActivity.ShowHtml_Html_Path_Key,
                                        ZZTConfig.HPZZT_USERINFO_URL);
                                SettingActivity.this.startActivity(intent1);
                            }
                        }));
        findViewById(R.id.setting_version_upgrade).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                SettingActivity.this.startActivity(new Intent(SettingActivity.this, AboutActivity.class));
                            }
                        }));
        findViewById(R.id.bt_setting_exit).setOnClickListener(
                CommonUtils.createNoQuickResponseViewListener(1000,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                doOut();
                            }
                        }));
        findViewById(R.id.setting_version_upgrade).setOnLongClickListener(
                new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View arg0) {
                        if (ZZTConfig.ENV.PRINTLOG) {
                            String logFilePath = StorageUtility.combinePath(
                                    getApplicationInfo().dataDir, "plainlog.txt");
                            String logFile = PlainLogger.instance(logFilePath)
                                    .getFileAbsolutePath();
                            String tempLogFile = Storage.getAppStorage()
                                    .getExternalPath("templog.txt");
                            HPLog.e(HPLog.FDL_TAG, "logFilePath:" + logFilePath);
                            HPLog.e(HPLog.FDL_TAG, "logFile:" + logFile);
                            HPLog.e(HPLog.FDL_TAG, "tempLogFile:" + tempLogFile);
                            try {
                                StorageUtility.copyFile(new File(logFile),
                                        new File(tempLogFile));
                                Intent intent = SystemUtility
                                        .createSendEmailIntent("472477640@qq.com",
                                                "BlueOffice crash log", tempLogFile);
                                startActivity(intent);
                            } catch (Exception ex) {
                                HPLog.e("SettingFragment",
                                        "Failed to send log.", ex);
                                return false;
                            }
                        }
                        return false;
                    }
                });
    }

    /***
     * 账账通账户退出
     */
    private void doOut() {
        if (AccountUtils.getAccountPhone(this) == null)
            return;
        Hashtable<String, String> params = new Hashtable<>();
        params.put("phone", SecureManager.getInstance().des(AccountUtils.getAccountPhone(this), 1, null));
        params.put("channel", ClientEngine.CHANNEL);
//        doJsonPost(this, DO_zztAccountExit, ResponseBean.class, params);
        new AccountExitModel().doAccountLoginPost(this, params, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                if (bean.getResponseCode().equals("0")) {
                    exitAccount();
                }
            }
        });
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(getString(R.string.new_setting_title), true);
    }
}
